﻿app.controller('NoticeController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.InitImgeUpload3 = function () {
        setTimeout(function () {
            for (var i = 0; i < $scope.ImageCount.length; i++) {
                var imagebox = "#imagebox_" + $scope.ImageCount[i];
                var dragImgUpload = new DragImgUpload(imagebox, {
                    callback: function (files) {
                        //回调函数，可以传递给后台等等
                        var file = files[0];
                        Upload.upload({
                            method: 'POST',
                            url: '../Common/UploadFormImage',
                            data: { "Category": 'Notice/Image' },
                            file: file
                        })
                            .success(function (ret) {
                                $scope.Submiting = false;
                                if (ret.Code == 100) {
                                    $scope.ImageList.push(ret.Data);
                                } else {

                                }

                            })
                            .error(function (data, status, headers, config) {
                                common.error($scope);
                            })
                    }
                }, "", $scope.ImageCount[i]);
            }
        }, 1000);
    }

    $scope.InitImgeUpload4 = function () {
        setTimeout(function () {
            for (var i = 0; i < $scope.ImageCount.length; i++) {
                var imagebox = "#videobox_" + $scope.ImageCount[i];
                var dragImgUpload = new DragImgUpload(imagebox, {
                    callback: function (files) {
                        //回调函数，可以传递给后台等等
                        var file = files[0];
                        Upload.upload({
                            method: 'POST',
                            url: '../Common/UploadFormImage',
                            data: { "Category": 'Notice/Video' },
                            file: file
                        })
                            .success(function (ret) {
                                $scope.Submiting = false;
                                if (ret.Code == 100) {
                                    $scope.FileList.push(ret.Data);
                                } else {

                                }

                            })
                            .error(function (data, status, headers, config) {
                                common.error($scope);
                            })
                    }
                }, "", $scope.ImageCount[i]);
            }
        }, 1000);
    }

    $scope.GetNoticeType = function () {
        $scope.NoticeTypeList = [];
        $http({
            method: 'POST',
            url: '../Notice/GetNoticeType'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.NoticeTypeList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                mui.toast("请检查网络");
            });
    }


    $scope.AddNotice = function (model,status) {
        model.NoticeContent = $("#noticecontent").code();
        model.Status = status;
        var videofiles = document.getElementById("videofiles").files;
        var docfiles = document.getElementById("docfiles").files;
        var formData =[];

        // 添加类目为 images 的文件  
        for (var i = 0; i < videofiles.length; i++) {
            formData.push({
                'videofile': videofiles[i]
            });
        }

        // 添加类目为 docfiles 的文件
        for (var j = 0; j < docfiles.length; j++) {
            formData.push({
                'docfile': docfiles[j]
            });
        }
        Upload.upload({
            method: 'POST',
            url: '../Notice/AddNoiceForm',
            data: { 'model': model, "Imagelist": $scope.ImageList },
            file: formData
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("通知公告添加成功！");
                    setTimeout(function () {
                        location.href = "NoticePageList?page=1";
                    }, 2000)
                } else
                    alertify.alert(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.EditNotice = function (model, status,page) {
        model.NoticeContent = $("#noticecontent").code();
        model.Status = status;
        var videofiles = document.getElementById("videofiles").files;
        var docfiles = document.getElementById("docfiles").files;
        var formData = [];

        // 添加类目为 images 的文件  
        for (var i = 0; i < videofiles.length; i++) {
            formData.push({
                'videofile': videofiles[i]
            });
        }

        // 添加类目为 docfiles 的文件
        for (var j = 0; j < docfiles.length; j++) {
            formData.push({
                'docfile': docfiles[j]
            });
        }
        Upload.upload({
            method: 'POST',
            url: '../Notice/EditNoiceForm',
            data: { 'model': model, "Imagelist": $scope.ImageList },
            file: formData
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("通知公告修改成功！");
                    setTimeout(function () {
                        location.href = "NoticePageList?page=" + page;
                    }, 2000)
                } else
                    alertify.alert(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.GetNoticeFormPageList = function (model, page) {
        $scope.NoticeFormList = [];
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../Notice/GetNoticeFormPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.NoticeFormList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }
    $scope.GetNoticeDetail = function (id,edit) {
        $scope.NoticeDetail = {};
        $http({
            method: 'POST',
            url: '../Notice/GetNoticeDetail',
            data: { "Id": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.NoticeDetail = ret.Data;
                    $scope.NoticeDetail.NoticeType = $scope.NoticeDetail.NoticeType + "";
                    $scope.NoticeDetail.CreateTime = $filter('JSONDate')($scope.NoticeDetail.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    $scope.NoticeDetail.PublishTime = $filter('JSONDate')($scope.NoticeDetail.PublishTime, 'yyyy-MM-dd');
                    $('#noticecontent').code($scope.NoticeDetail.NoticeContent);
                    if (!edit) {
                        $("#noticecontent").html($scope.NoticeDetail.NoticeContent);
                    }

                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.DeleteNoiceForm = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Notice/DeleteNoiceForm',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetNoticeFormPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.UpdateStatusNotice = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../Notice/UpdateStatusNotice',
                    data: { 'Id': id,'status':0 }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("操作成功！");
                            $scope.GetNoticeFormPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.DeleteNoticeFile = function (Id, filelist) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该文件？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Notice/DeleteNoticeFile',
                    data: {  'Id': Id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        if (ret.Code == 100) {
                            for (var i = 0; i < filelist.length; i++) {
                                if (filelist[i].FileId == Id) {
                                    filelist.splice(i, 1);
                                }
                            }
                            alertify.alert("删除成功！");
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        $scope.PageLoading = false;
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }
});